import axios from 'axios';
import QS from 'qs';

const urls = {
    flims: "/filmApi/loadFilms.php",
    types: "/filmApi/loadTyps.php",
    detail: "/filmApi/loadFilmById.php",
    comment: "/filmApi/loadComments.php",
    addComment: "/filmApi/addComment.php"
}

const baseUrls = {
    production: "http://127.0.0.1:80",
    development: "/api"
}
axios.defaults.baseURL = baseUrls[process.env.NODE_ENV];

const errorFun = function (error) {
    console.log("------->请求失败:", error); // 开发者查看
    window.$showTip("网络连接失败，请稍后重试"); // 完成
}

const loadFilms = function (params) {
    return axios.get(urls.flims, {
        params: params
    }).then((response) => {
        if (response.statusText != "OK") {
            return Promise.reject(new Error("服务出现故障"));
        }
        if (!response.data.resultState) {
            return Promise.reject(new Error(response.data.msg));
        }
        return response.data.result
    });
}

const loadTypes = function (params) {
    return axios.get(urls.types, {
        params: params
    }).then((response) => {
        if (response.statusText != "OK") {
            return Promise.reject(new Error("服务出现故障"));
        }
        if (!response.data.resultState) {
            return Promise.reject(new Error(response.data.msg));
        }
        return response.data.result
    });
}

const loadFilmById = function (params) {
    //用post，用了第二个参数，第三个参数也要传
    return axios.post(urls.detail, QS.stringify(params), {
        baseURL: process.env.NODE_ENV === 'production' ? "http://127.0.0.1:80" : "/api"
    }).then((response) => {
        if (response.statusText != "OK") {
            return Promise.reject(new Error("服务出现故障"));
        }
        if (!response.data.resultState) {
            return Promise.reject(new Error(response.data.msg));
        }
        return response.data.result
    });
}


const loadComments = function (params) {
    return axios.get(urls.comment, {
        params: params
    }).then((response) => {
        if (response.statusText != "OK") {
            return Promise.reject(new Error("服务出现故障"));
        }
        if (!response.data.resultState) {
            return Promise.reject(new Error(response.data.msg));
        }
        return response.data.result
    });
}

const pushComment = function (params) {
    return axios.post(urls.addComment, QS.stringify(params), {
        baseURL: process.env.NODE_ENV === 'production' ? "http://127.0.0.1:80" : "/api"
    }).then((response) => {
        if (response.statusText != "OK") {
            return Promise.reject(new Error("服务出现故障"));
        }
        if (!response.data.resultState) {
            return Promise.reject(new Error(response.data.msg));
        }
        return response.data.msg
    });
}

export default {
    install: function (Vue) {
        Vue.prototype.$api = {
            loadFilms,
            loadTypes,
            loadFilmById,
            loadComments,
            pushComment
        };
        Vue.prototype.$error = errorFun
    }
}